<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    //数组去重
    //1.第一种方法去重(简单项)
    var arr = ['true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a', 'a', {}, {}];
    var arr1 = arr.filter(function (item, index, arr) {
      //当前元素，在原始数组中的第一个索引==当前索引值，否则返回当前元素
      return arr.indexOf(item, 0) === index;
    });
    //console.log(arr1)
    /***************************************************************************************************************/
    //2.第二种方法去重(数组对象)
    var arr2 = [
      { id: 0, name: '小明' },
      { id: 1, name: '小红' },
      { id: 2, name: '小东' },
      { id: 3, name: '小张' },
      { id: 0, name: '小李' },
      { id: 1, name: '小王' }
    ]
    var obj = {};
    var arr3 = arr2.reduce(function (prev, value, i, arr) {
      obj[value.id] ? "" : obj[value.id] = true && prev.push(value)
      // console.log(obj)
      return prev
    }, [])
    //console.log(arr3)
    /***************************************************************************************************************/
    //两个数组去重问题(案例：页面第一次选完数据后，未提交，第二次选择重复的数据)只需要两个数组，不需要第三个数组
    var arr14 = [{ id: 1, name: '小米' }, { id: 2, name: '华为' }, { id: 3, name: '苹果' }, { id: 4, name: 'vivo' }, { id: 5, name: 'oppo' }]
    var arr15 = [{ id: 1, name: '小米' }, { id: 2, name: '华为' }, { id: 6, name: '魅族' }]
    for (const i of arr15) {
      var isNew = false;//是否为新元素
      arr14.forEach(function (j, index, arr) {
        if (j.id == i.id) {
          isNew = true
        }
        if (index == arr.length - 1 && isNew == false) {//当里面的循环到了最后一个位置，isNew为false，说明该元素是新元素
          arr14.push(i)
        }
      });
    }
    console.log(arr14);
    /***************************************************************************************************************/
    //同上的第二种方法
    var arr16 = [{ id: 1, name: '小米' }, { id: 2, name: '华为' }, { id: 3, name: '苹果' }, { id: 4, name: 'vivo' }, { id: 5, name: 'oppo' }]
    var arr17 = [{ id: 1, name: '小米' }, { id: 2, name: '华为' }, { id: 6, name: '魅族' }]
    arr16.forEach(e => {
      arr17.forEach(function (m, i, arr) {
        if (e.id == m.id) {
          arr17.splice(i, 1)
        }
      })
    });
    console.log(arr16.concat(arr17));
    /************************************************************************************************************
        * 数组去重,利用api(选择数据后,添加到原始数组的情况,页面中选择数据基本都是这种操作)
       */
    var arr15 = [
      { id: 1, name: '小红' },
      { id: 2, name: '小东' },
      { id: 3, name: '小张' },
      { id: 4, name: '小王' }
    ]
    var arr16 = arr15.map(el => el.id)
    var arr17 = [
      { id: 5, name: '小智' },
      { id: 6, name: '小源' },
      { id: 1, name: '小红' },]
    arr17.forEach(el => {
      if (!arr16.includes(el.id)) {
        arr15.push(el)
      }
    })
    console.log(arr15);
    /***************************************************************************************************************/
    //3.第三种去重(hash原理) 只能是数字去重
    var arr4 = [1, 2, 3, 2, 1, 4];
    var hash = {};//一个对象中不允许有重复的下标()
    for (var i = 0; i < arr4.length; i++) {
      hash[arr4[i]] = 1;
    }
    var res = [];
    var i = 0;
    for (res[i++] in hash);//i++ 先执行操作  再加一
    //console.log(res);
    /***************************************************************************************************************/
    //4.第四种方法(最简单)
    var arr5 = [1, 2, "1", "1", "hello", "hello", 3, 1, 2]
    //console.log(Array.from(new Set(arr5)))
    //console.log([...new Set(arr5)])
    console.log(Array.from("RUNOOB"));//['R', 'U', 'N', 'O', 'O', 'B']
    /***************************************************************************************************************/
    //将数组里面的数字随机乱序排列
    var arr6 = [1, 2, 3, 4, 5, 6]
    for (var i = 0; i < arr6.length; i++) {
      var m = Math.floor(Math.random() * 6)//0到5之间的随机数
      var num = arr6[i]//原始类型值  传递的是原来的一份副本
      arr6[i] = arr6[m]
      arr6[m] = num
    }
    //console.log(arr6)
    /***************************************************************************************************************/
    //将对象拆分转成数组(一个对象数组)
    var person = { lilei: '李磊', age: 12, address: '成都' }
    //将对象拆分转成数组(多个对象数组)
    var keys = Object.keys(person)
    var arra = [];
    for (var i = 0; i < keys.length; i++) {
      arra.push({})
      arra[i][keys[i]] = person[keys[i]]
    }
    //console.log(arra)
    /***************************************************************************************************************/
    //数组find方法，和map不同的是，它返回的是找到的值，而不是返回一个数组
    var arr = [
      { name: 'jerry', sex: 'man' },
      { name: 'jack', sex: 'woman' },
      { name: 'jack', sex: 'man' }
    ]
    var jack = arr.find(function (obj) {
      return obj.name == 'jack';
    })
    //console.log(jack);  // {name: "jack", sex: "woman"}

    var bill = arr.find(function (obj) {
      return obj.name == 'bill';
    })
    //console.log(bill);  // undefined
    /***************************************************************************************************************/
    //数组递归遍历json数据
    var arr11 = [
      { name: '小明', age: 25, addr: '兰州', children: [{ name: '小红', age: 26, children: [{ name: '红红', age: 10 }] }] },
      { name: '小兰', age: 20, addr: '刘家峡', children: [{ name: '小丽', age: 21, children: [{ name: '亮亮', age: 11 }] }] },
      { name: '小胖', age: 23, children: [{ name: '小智', age: 24, addr: '海石湾', children: [{ name: '东东', age: 12 }] }] },
      { name: '小高', age: 15, children: [{ name: '小云', age: 36, children: [{ name: '然然', age: 13 }] }] }
    ]
    var oldArr = [];
    function forArr(arr) {
      var newArr = [];
      arr.forEach(e => {
        var obj = {};
        for (const key in e) {
          if (Object.hasOwnProperty.call(e, key)) {
            if (Array.isArray(e[key])) {
              forArr(e[key])
            } else {
              obj[key] = e[key]
            }
          }
        }
        oldArr.push(obj)
      });
      newArr = newArr.concat(oldArr)
      return newArr
    }
    //console.log(forArr(arr11))
    //console.log(oldArr);
    /***************************************************************************************************************/
    //相同结构数组组合
    var arr12 = ['张总', 18394488802, '刘总', 15693452104, '李总', 13984023652];
    var arr13 = [];
    var obj1;
    arr12.forEach(function (e, i) {
      if (i % 2 == 0) {
        obj1 = {}
        obj1.name = e;
      } else {
        obj1.telphone = e;
        arr13.push(obj1)
      }
    })
    // console.log(arr13);
    /***************************************************************************************************************/
    /************************************************************************************************************
     * id pid 转为children格式(树形式)
    */
    var source = [
      { id: 1, pid: 0, value: "唐僧" },
      { id: 2, pid: 1, value: "唐僧1" },
      { id: 3, pid: 1, value: "唐僧2" },
      { id: 4, pid: 1, value: "唐僧3" },
      { id: 5, pid: 0, value: "孙悟空" },
      { id: 6, pid: 5, value: "孙悟空1" },
      { id: 7, pid: 5, value: "孙悟空2" },
      { id: 8, pid: 5, value: "孙悟空3" },
      { id: 9, pid: 0, value: "猪八戒" },
      { id: 10, pid: 9, value: "猪八戒1" },
      { id: 11, pid: 10, value: "猪八戒2" },
      { id: 12, pid: 11, value: "猪八戒3" },
      { id: 13, pid: 12, value: "猪八戒4" },
      { id: 14, pid: 13, value: "猪八戒5" },
      { id: 15, pid: 14, value: "猪八戒6" },
      { id: 16, pid: 0, value: "沙僧" }
    ]
    function toTree(data) {
      let result = []
      let map = {};
      data.forEach(item => {
        map[item.id] = item;
      });
      data.forEach((item, i) => {
        let parent = map[item.pid];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    }
    // console.log(toTree(source))

    /************************************************************************************************************
        * 将某个属性进行排序
       */
    var arr14 = [{ name: '张飞', age: 34 }, { name: '关羽', age: 30 }, { name: '刘备', age: 60 },]
    function sortAge(key) {
      return function (a, b) {
        return a[key] - b[key]
      }
    }
    arr14.sort(sortAge('age'))
    console.log(arr14.sort(sortAge('age')));
    /*************************************************************************
    数组生成随机数,不重复
    */
    function get() {
      var arr = [];
      for (let i = 0; i < 5; i++) {
        var n = parseInt(Math.random() * 5);
        if (arr.includes(n)) {
          i--;
        } else {
          arr.push(n);
        }
      }
      console.log(arr);
    }
    /***************************************************************************
    *数组方法 flatMap
    **/
    var arr18 = [{ name: 'xixi', children: [{ age: 12, addr: 'beijin' }] }]
    var arr19 = arr18.flatMap(el => el.children)
    console.log(arr19);//[{age:12,addr:'beijin'}]
    /***********************************************************************
     * 冒泡排序*/
    function bubbleSort(arr) {
      for (let i = 0; i < arr.length; i++) {
        let flag = true;
        for (let j = 0; j < arr.length - i - 1; j++) {
          if (arr[j] > arr[j + 1]) {
            flag = false;
            let temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
          }
        }
        if (flag) break;
      }
      return arr;
    }
    bubbleSort([5, 1, 3, 4, 9, 8, 10]) //[1, 3, 4, 5, 8, 9, 10]

    /********************************************************************************
     * 数组递归求和
     * */
    var arr20 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    function sum(arr, i) {
      if (arr.length - 1 == i) return 0;
      return arr[i] + sum(arr, i + 1)
    }
    console.log(sum(arr20, 0));
    //递归转函数
    function sum1() {
      var i = 0, s = 0;
      function _sum1() {
        if (arr20.length - 1 == i) return;
        s += arr20[i]
        i += 1
        _sum1()
      }
      _sum1()
      console.log(s);
    }
    sum1()
  </script>
</body>

</html>